﻿//Copyright (c) Microsoft Corporation. All rights reserved.

using Microsoft.Ted.Wacel;
using System;
using System.Collections.Generic;
using System.Threading;

namespace Microsoft.Ted.Wacel.Demo.Worker
{
    public class RandomCube
    {
        private bool mShouldRun = true;
        private Thread mThread;
        private static Random mRand = new Random();
        public void Start()
        {
            Cache.Cube cube = new Cache.Cube("RandomCube");
            cube.AddDimension(Dimension.GetTimeDimensionMonthDayHour5Minutes(DateTime.Now.Year));
            cube.AddDimension(Dimension.CreateFreeDimension("Slot"));
            mThread = new Thread(new ThreadStart(() =>
                {
                    while (mShouldRun)
                    {
                        List<dynamic> datapoints = new List<dynamic>();
                        for (int i = 0; i < 4; i++)
                        {
                            var now = DateTime.UtcNow;
                            now = new DateTime(now.Year, now.Month, now.Day, now.Hour, 0,0);
                            for (int j = 0; j < 12; j++)
                            {
                                datapoints.Add(new { Time = Dimension.Get5MinuteTimeKey(now.AddMinutes(j*5)), Slot = "Slot" + i.ToString(), Value = mRand.NextDouble() * 90 +10});
                            }
                        }
                        cube.Update(datapoints);
                        Thread.Sleep(500);
                    }
                }));
            mThread.Start();
        }
        public void Stop()
        {
            mShouldRun = false;
            mThread.Join();
        }
    }
}
